package htrun

import (
	"core/auth"
	"core/coreentity"
	"core/coreservice"
	"core/corestruct"
	"core/coreutil"
	"core/datetime"
	"core/db"
	"gorm.io/gorm"
)

type dbSetup struct {
	ctx   *corestruct.HttpContext
	lpage *[]coreentity.HtPage
	lact  *[]coreentity.HtAction
}

func (p *dbSetup) Setup(ctx1 *corestruct.HttpContext) error {
	p.ctx = ctx1

	err := p.HtUser()
	if err != nil {
		return err
	}

	err = p.HtDept()
	if err != nil {
		return err
	}

	err = p.HtRole()
	if err != nil {
		return err
	}

	err = p.HtPlat()
	if err != nil {
		return err
	}

	err = p.HtPage()
	if err != nil {
		return err
	}

	return nil
}

func (p *dbSetup) HtUser() error {
	var muser = &coreentity.HtUser{
		Id:       1000,
		Name:     "后台管理员",
		Ename:    "htadmin",
		Pwd:      auth.CreatePwd("htadmin", "111111"),
		Czpwd:    "",
		Mobile:   "15655554566",
		Phone:    "",
		Sex:      "",
		Url:      "",
		Zw:       "",
		Birthday: datetime.Now(),
		Address:  "",
		Email:    "",
		IdCode:   "",
		Memo:     "",
		HtareaId: 0,
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx := db.DbMaster.Find(muser)
	if tx.RowsAffected > 0 {
		return nil
	}
	tx = db.DbMaster.Create(muser)
	if tx.Error != nil {
		return tx.Error
	}

	var mur = &coreentity.HtUserRole{
		Id:       1000,
		HtuserId: 1000,
		HtdeptId: 1000,
		HtroleId: 1000,
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx = db.DbMaster.Create(mur)
	if tx.Error != nil {
		return tx.Error
	}

	var mud = &coreentity.HtUserDept{
		Id:       1000,
		HtuserId: 1000,
		HtdeptId: 1000,
		State:    "启用",
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx = db.DbMaster.Create(mud)
	if tx.Error != nil {
		return tx.Error
	}
	return nil
}

func (p *dbSetup) HtDept() error {
	var m = &coreentity.HtDept{
		Id:         1000,
		Name:       "研发部门",
		Pid:        0,
		RootId:     0,
		HtplatId:   1000,
		HtareaId:   1,
		Address:    "",
		Dutyperson: "",
		Tel:        "",
		Memo:       "",
		Url:        "",
		State:      "",
		Lng:        0,
		Lat:        0,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}
	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) HtRole() error {
	var m = &coreentity.HtRole{
		Id:       1000,
		Name:     "开发管理",
		HtplatId: 1000,
		Memo:     "",

		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}

	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) HtPlat() error {
	var m = &coreentity.HtPlat{
		Id:   1000,
		Pid:  0,
		Name: "开发管理",
		Memo: "",
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}

	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) findPage(code string) *coreentity.HtPage {
	for index, _ := range *p.lpage {
		if (*p.lpage)[index].Code == code {
			return &((*p.lpage)[index])
		}
	}
	return nil
}
func (p *dbSetup) findAct(url string) *coreentity.HtAction {
	for index, _ := range *p.lact {
		if (*p.lact)[index].FullUrl == url {
			return &((*p.lact)[index])
		}
	}
	return nil
}

func (p *dbSetup) HtPage() error {
	var tx = db.DbMaster.Model(&coreentity.HtPage{})
	var lstsvr = &coreservice.ListService[coreentity.HtPage]{}
	r, err := lstsvr.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lpage = r

	var lstsvract = &coreservice.ListService[coreentity.HtAction]{}
	ract, err := lstsvract.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lact = ract

	err = p.HtPagehtclsindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtactonindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtpageindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtcodelogindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtmenuindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtroleindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtuserindex()
	if err != nil {
		return err
	}

	err = p.HtPagehterrorindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtactionlogindex()
	if err != nil {
		return err
	}

	err = p.HtPagehtuserloginindex()
	if err != nil {
		return err
	}
	return nil
}
func (p *dbSetup) HtPagehtclsindex() error {
	var code = "/htcls/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "类型管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	var db = p.ctx.DB
	tx2 := coreservice.BaseService.Insert(mdb, db)
	return tx2.Error
}
func (p *dbSetup) HtPagehtactonindex() error {
	var code = "/htaction/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "API管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "刷新API",
		Code:       "refreshtodb",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htaction/refreshtodb",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}
func (p *dbSetup) HtPagehtpageindex() error {
	var code = "/htpage/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "功能页面"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "操作",
		Code:       "auth",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htpage/*",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}
func (p *dbSetup) HtPagehtcodelogindex() error {
	var code = "/htcodelog/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "研发日志"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	var db = p.ctx.DB
	tx2 := coreservice.BaseService.Insert(mdb, db)
	return tx2.Error
}
func (p *dbSetup) HtPagehtmenuindex() error {
	var code = "/htmenu/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "菜单管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "操作",
		Code:       "auth",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htmenu/*",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}
func (p *dbSetup) HtPagehtroleindex() error {
	var code = "/htrole/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "角色管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "操作",
		Code:       "auth",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htrole/*",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}
func (p *dbSetup) HtPagehtuserindex() error {
	var code = "/htuser/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "用户管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "操作",
		Code:       "auth",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htuser/*",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}

func (p *dbSetup) HtPagehterrorindex() error {
	var code = "/hterror/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "系统异常"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	var db = p.ctx.DB
	tx2 := coreservice.BaseService.Insert(mdb, db)
	return tx2.Error
}
func (p *dbSetup) HtPagehtactionlogindex() error {
	var code = "/htactionlog/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "操作日志"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	var db = p.ctx.DB
	tx2 := coreservice.BaseService.Insert(mdb, db)
	return tx2.Error
}
func (p *dbSetup) HtPagehtuserloginindex() error {
	var code = "/htuserlogin/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "在线用户"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "后台",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}

	var mauth = &coreentity.HtPageAuth{
		Id:         coreutil.IdUtil.GetId(),
		Name:       "操作",
		Code:       "auth",
		HtpageId:   mdb.Id,
		HtactionId: 0,
		Memo:       "",
		Urls:       "/htuserlogin/*",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var db = p.ctx.DB

	err := db.Transaction(func(tx *gorm.DB) error {
		tx2 := coreservice.BaseService.Insert(mdb, tx)
		if tx2.Error != nil {
			return tx2.Error
		}

		tx2 = coreservice.BaseService.Insert(mauth, tx)
		if tx2.Error != nil {
			return tx2.Error
		}
		return nil
	})
	return err
}
